﻿using Dapper;
using System;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepLineConfig;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkflowTemplate.WorkflowTemplateStepLineConfig
{
    /// <summary>
    /// 工作流模板步骤线配置
    /// </summary>
    public class WorkflowTemplateStepLineConfigData : IWorkflowTemplateStepLineConfigData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workflowtemplatesteplineconfig";
        private const string QUERY = @"SELECT WorkflowTemplateStepLineConfigId,ColorStr,StartId,StartPointId,StartDirection,StartCoordinateX,StartCoordinateY,EndId,EndPointId,EndDirection,EndCoordinateX,TemplateCode,EndCoordinateY,SvgLineId,PathsStr,TemplateName,TemplateStepCode,StepLineCode,Id,PathType,PathTypeStr,Color FROM oa_workflowtemplatesteplineconfig";
        public WorkflowTemplateStepLineConfigData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查工作流模板步骤线配置是否存在
        public async Task<bool> Exists(long workflowTemplateStepLineConfigId)
        {
            string sql = string.Format(@"{0} WHERE WorkflowTemplateStepLineConfigId=?WorkflowTemplateStepLineConfigId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowTemplateStepLineConfigId", workflowTemplateStepLineConfigId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加工作流模板步骤线配置
        public async Task<WorkflowTemplateStepLineConfigEntity> Add(WorkflowTemplateStepLineConfigEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("oa_workflowtemplatesteplineconfig", new string[] { "ColorStr", "StartId", "StartPointId", "StartDirection", "StartCoordinateX", "StartCoordinateY", "EndId", "EndPointId", "EndDirection", "EndCoordinateX", "TemplateCode", "EndCoordinateY", "SvgLineId", "PathsStr", "TemplateName", "TemplateStepCode", "StepLineCode", "Id", "PathType", "PathTypeStr", "Color" });
            sql = string.Format("{0} {1} WHERE WorkflowTemplateStepLineConfigId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?ColorStr", entity.ColorStr?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?StartId", entity.StartId?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?StartPointId", entity.StartPointId?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?StartDirection", entity.StartDirection.ToString(), DbType.AnsiString);
            parameters.Add("?StartCoordinateX", entity.StartCoordinateX, DbType.Double);
            parameters.Add("?StartCoordinateY", entity.StartCoordinateY, DbType.Double);
            parameters.Add("?EndId", entity.EndId?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?EndPointId", entity.EndPointId?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?EndDirection", entity.EndDirection.ToString(), DbType.AnsiString);
            parameters.Add("?EndCoordinateX", entity.EndCoordinateX, DbType.Double);
            parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?EndCoordinateY", entity.EndCoordinateY, DbType.Double);
            parameters.Add("?SvgLineId", entity.SvgLineId?.Trim(), DbType.AnsiString, size: 200);
            parameters.Add("?PathsStr", entity.PathsStr?.Trim(), DbType.String);
            parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?TemplateStepCode", entity.TemplateStepCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?StepLineCode", entity.StepLineCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Id", entity.Id?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?PathType", entity.PathType.ToString(), DbType.AnsiString);
            parameters.Add("?PathTypeStr", entity.PathTypeStr?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?Color", entity.Color?.Trim(), DbType.AnsiString, size: 10);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepLineConfigEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改工作流模板步骤线配置
        public async Task<WorkflowTemplateStepLineConfigEntity> Update(WorkflowTemplateStepLineConfigEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("oa_workflowtemplatesteplineconfig", new string[] { "ColorStr", "StartId", "StartPointId", "StartDirection", "StartCoordinateX", "StartCoordinateY", "EndId", "EndPointId", "EndDirection", "EndCoordinateX", "TemplateCode", "EndCoordinateY", "SvgLineId", "PathsStr", "TemplateName", "TemplateStepCode", "StepLineCode", "Id", "PathType", "PathTypeStr", "Color" }, new string[] { "WorkflowTemplateStepLineConfigId" });
            sql = string.Format("{0} {1} WHERE WorkflowTemplateStepLineConfigId=?WorkflowTemplateStepLineConfigId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowTemplateStepLineConfigId", entity.WorkflowTemplateStepLineConfigId, DbType.Int64, size: 20);
            parameters.Add("?ColorStr", entity.ColorStr?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?StartId", entity.StartId?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?StartPointId", entity.StartPointId?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?StartDirection", entity.StartDirection.ToString(), DbType.AnsiString);
            parameters.Add("?StartCoordinateX", entity.StartCoordinateX, DbType.Double);
            parameters.Add("?StartCoordinateY", entity.StartCoordinateY, DbType.Double);
            parameters.Add("?EndId", entity.EndId?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?EndPointId", entity.EndPointId?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?EndDirection", entity.EndDirection.ToString(), DbType.AnsiString);
            parameters.Add("?EndCoordinateX", entity.EndCoordinateX, DbType.Double);
            parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?EndCoordinateY", entity.EndCoordinateY, DbType.Double);
            parameters.Add("?SvgLineId", entity.SvgLineId?.Trim(), DbType.AnsiString, size: 200);
            parameters.Add("?PathsStr", entity.PathsStr?.Trim(), DbType.String);
            parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?TemplateStepCode", entity.TemplateStepCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?StepLineCode", entity.StepLineCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Id", entity.Id?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?PathType", entity.PathType.ToString(), DbType.AnsiString);
            parameters.Add("?PathTypeStr", entity.PathTypeStr?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?Color", entity.Color?.Trim(), DbType.AnsiString, size: 10);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepLineConfigEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除工作流模板步骤线配置
        public async Task<bool> Delete(long workflowTemplateStepLineConfigId)
        {
            string sql = @"DELETE FROM oa_workflowtemplatesteplineconfig WHERE WorkflowTemplateStepLineConfigId=?WorkflowTemplateStepLineConfigId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowTemplateStepLineConfigId", workflowTemplateStepLineConfigId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        public async Task<bool> Delete(string templateCode)
        {
            string sql = @"DELETE FROM oa_workflowtemplatesteplineconfig WHERE TemplateCode=?TemplateCode";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TemplateCode", templateCode, DbType.AnsiString, size: 50);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取工作流模板步骤线配置
        public async Task<WorkflowTemplateStepLineConfigEntity> GetEntity(long workflowTemplateStepLineConfigId)
        {
            string sql = string.Format(@"{0} WHERE WorkflowTemplateStepLineConfigId=?WorkflowTemplateStepLineConfigId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowTemplateStepLineConfigId", workflowTemplateStepLineConfigId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowTemplateStepLineConfigEntity>(sql, parameters);
        }
        #endregion

        #region 获取工作流模板步骤线配置列表
        public async Task<IEnumerable<WorkflowTemplateStepLineConfigEntity>> GetList(string templateCode)
        {
            string sql = string.Format(@"{0} WHERE TemplateCode=?TemplateCode", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TemplateCode", templateCode, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.QueryAsync<WorkflowTemplateStepLineConfigEntity>(sql, parameters);
        }
        #endregion

        #region 获取工作流模板步骤线配置分页
        public async Task<(IEnumerable<WorkflowTemplateStepLineConfigEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workflowtemplatesteplineconfig {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "WorkflowTemplateStepLineConfigId" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowTemplateStepLineConfigEntity, int>(sql, parameters);
        }
        #endregion
    }
}



